package com.mf.morefun.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ProjectName：WisdomBus
 * PackageName：com.zhcx.wisdombus.base
 * description:
 * Date：2016/9/5 9:35
 * Author：Steven
 * ClassName:BaseBusFragment
 **/
public abstract class BaseBusFragment extends Fragment {
    protected View mViewLayout;
    protected Context mContext;
    protected Unbinder unbinder;//绑定

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (null == this.mViewLayout) {
            this.mViewLayout = inflater.inflate(this.getContentLayoutId(), container, false);
        }
        if (null != this.mViewLayout.getParent()) {
            ViewGroup parent = (ViewGroup) this.mViewLayout.getParent();
            parent.removeView(this.mViewLayout);
        }
        mContext = this.getActivity();
        unbinder = ButterKnife.bind(this, mViewLayout);
        return mViewLayout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.initView(this.mViewLayout, savedInstanceState);
        this.initData();
    }

    /**
     * 视图id
     *
     * @return layout id
     */
    protected abstract int getContentLayoutId();

    /**
     * 初始化控件
     *
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);

    /**
     * Initialize the Activity data
     */
    protected abstract void initData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解除绑定，官方文档只对fragment做了解绑
        if (null != unbinder){
            unbinder.unbind();
        }
    }
}
